/*
 * @Author: jhw jiaxianshengye@gmail.com
 * @LastEditTime: 2025-07-30 19:29:39
 * @LastEditors: jhw
 * @Description: 文件描述
 */
export const exportExcel2 = (initColumns: any, initList: any, name: string) => {
  let str: string = ''
  const arr: string[] = []
  const keyArr: string[] = []
  initColumns.forEach((item: any) => {
    arr.push(item['title'])
    keyArr.push(item['key'])
  })
  str = arr.join(',') + '\n'
  for (let i = 0; i < initList.length; i++) {
    const subArr = keyArr.map((item) => {
      const temp = initList[i][item] === undefined ? '' : initList[i][item] + '' // 转成字符串
      // 下面解决中文乱码问题 会把空格转成 #, 这里把空格转成 \xa0 就不会有这个问题了
      return temp ? temp.replaceAll(/\s+/g, '\xa0') : ''
    })
    let subStr = subArr.join('\t,')
    subStr += '\n'
    str += subStr
  }
  // \ufeff 解决中文乱码问题
  const blob = new Blob(['\ufeff', str], { type: 'text/plain' })
  const object_url = window.URL.createObjectURL(blob)
  const link = document.createElement('a')
  link.href = object_url
  link.download = `${name}.csv`
  document.body.appendChild(link)
  link.click()
  document.body.removeChild(link)
}
